package com.itheima.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.PmsProductMapper;
import com.itheima.mapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.pojo.SmsFlashPromotion;
import com.itheima.pojo.SmsFlashPromotionProductRelation;
import com.itheima.pojo.db.PmsProduct;
import com.itheima.vo.PageResult;
import com.itheima.vo.SmsFlashPromotionProductRelationVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lenovo
 * @description 针对表【sms_flash_promotion_product_relation(商品限时购与商品关系表)】的数据库操作Service实现
 * @createDate 2022-10-11 14:26:17
 */
@DubboService
public class SmsFlashPromotionProductRelationApiImpl implements SmsFlashPromotionProductRelationApi {

    @Autowired
    private SmsFlashPromotionProductRelationMapper flashPromotionProductRelationMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    //批量选择商品添加关联
    @Override
    public void create(SmsFlashPromotionProductRelation flashPromotionProductRelation) {
        //productId
        //promotionId
        //promotionSessionId
        flashPromotionProductRelationMapper.insert(flashPromotionProductRelation);
    }

    //删除关联
    @Override
    public void delete(Long id) {
        flashPromotionProductRelationMapper.deleteById(id);
    }

    //分页查询不同场次关联及商品信息
    @Override
    public PageResult list(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        //分页构造函数
        //Params:
        //current – 当前页
        //size – 每页显示条数
        IPage<SmsFlashPromotionProductRelation> iPage = new Page<>(pageNum, pageSize);
        //条件
        LambdaUpdateWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(flashPromotionId), SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId)
                .eq(ObjectUtil.isNotNull(flashPromotionSessionId), SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId)
                .orderByAsc(SmsFlashPromotionProductRelation::getSort);
        //查询
        flashPromotionProductRelationMapper.selectPage(iPage, lqw);
        Integer total = Convert.toInt(iPage.getTotal());//总条数
        Integer totalPage = Convert.toInt(iPage.getPages());//当前分页总页数
        long size = iPage.getSize();//每页显示条数
        List<SmsFlashPromotionProductRelation> list = iPage.getRecords();
        //重新构建返回的list
        //拷贝
        SmsFlashPromotionProductRelationVo relationVo = new SmsFlashPromotionProductRelationVo();
        List<SmsFlashPromotionProductRelationVo> relationVoList =
                list.stream().map(relation -> {
                    BeanUtil.copyProperties(relation, relationVo);
                    //处理没有办法拷贝的  product
                    PmsProduct product = pmsProductMapper.selectById(relation.getProductId());
                    //如果这个商品存在的话
                    if (ObjectUtil.isNotNull(product)) {
                        relationVo.setProduct(product);
                    }
                    return relationVo;
                }).collect(Collectors.toList());

        return new PageResult(pageNum, pageSize, total, totalPage, relationVoList);
    }


    //修改关联信息
    @Override
    public void update(Long id, SmsFlashPromotionProductRelation flashPromotionProductRelation) {
        flashPromotionProductRelationMapper.updateById(flashPromotionProductRelation);
    }

    //根据id找relation
    @Override
    public SmsFlashPromotionProductRelation findById(Long id) {
        return flashPromotionProductRelationMapper.selectById(id);
    }
}




